var express = require('express');
var router = express.Router();
var mongo = require("./db")
var mongodb = require("mongodb");
var ObjectId = mongodb.ObjectId;
/* GET users listing. */
router.get('/', function (req, res, next) {
  res.send('respond with a resource');
});
// 添加接口
router.post("/addAndUpdate", function (req, res) {
  var obj = {
    email: req.body.email,
    password: req.body.password,
    sex: req.body.sex,
    aihao: req.body.aihao,
  }
  if (req.body.action == "add") {
    mongo("insert", "list", obj, function () {
      res.send({ success: "添加成功" })
    })
  } else if (req.body.action == "update") {
    mongo("update", "list", [{ _id: ObjectId(req.body._id) }, obj], function () {
      res.send({ success: "修改成功" })
    })
  }

})
// 查询接口
router.post("/getlist", function (req, res) {
  mongo("find", "list", {}, function (data) {
    res.send(data)
  })
})
// 删除接口
router.post("/del", function (req, res) {
  mongo("del", "list", { _id: ObjectId(req.body._id) }, function () {
    res.send({ success: "删除成功" })
  })
})
module.exports = router;
